#include "Player.h"

Player::Player(core::stringw pName, int mainAttrib, int rank)
{
	s_PlayerName = pName;
	m_AttribMath = ATTRIB_START_VALUE;
	m_AttribScience = ATTRIB_START_VALUE;
	m_AttribPE = ATTRIB_START_VALUE;
	m_MainAttribute = mainAttrib;
	switch(m_MainAttribute)
	{
	case ATTRIB_MATH:
		m_Atk = m_AttribMath;
		break;
	case ATTRIB_SCIENCE:
		m_Atk = m_AttribScience;
		break;
	case ATTRIB_PE:
		m_Atk = m_AttribPE;
		break;
	}
	n_Rank = rank;
	n_curWinStreak = NULL;
	m_MaxHP = HEALTH_START_VALUE;
	m_HealthPoints = m_MaxHP;
	m_MaxMP = MANA_START_VALUE;
	m_ManaPoints  = m_MaxMP;
	m_Speed = SPEED_START_VALUE;
	m_Atk = ATTACK_START_VALUE;
	n_CurrentDayPoints = DEFAULT_DAILY_POINT;
	m_IsKO = false;
	m_isDoneRankChange = false;
	bMove = core::rect<s32>(boxmovex, boxmovey, boxmovex + boxmovew, boxmovey + boxmoveh);
	bHit  = core::rect<s32>(boxhitx, boxhity, boxhitx + boxhitw, boxhity + boxhith);
	bIdle = core::rect<s32>(boxidlex, boxidley, boxidlex + boxidlew, boxidley + boxidleh);
	bAttack = core::rect<s32>(boxattackx, boxattacky, boxattackx + boxattackw, boxattacky + boxattackh);
	bSkill = core::rect<s32>(0,0,0,0);
	bPlay = bIdle;
	position = core::rect<s32>(0,0,0,0);
	max_move = MAX_MOV; //get value from save file if implemented
	max_attack = MAX_ATK; //get value from save file if implemented
	cur_move = max_move;
	cur_attack = max_move;
	movTimeLapse = 3000;//get value from save file if implemented
	atkTimeLapse = 2000;//get value from save file if implemented
	sprX = 0;
	sprY = 0;
	atkcooldown = 300; //Get from save file
	movcooldown = 300; //Get from save file
	hitcooldown = 300; //Get from save file
	moveTime = NULL;
	attackTime = NULL;
	hitTime = NULL;
	isHit = false;
	u32 movregtime;
	u32 atkregtime;
	m_isMovregen = false;
	m_isAtkregen = false;
	playerSpr = NULL;
	ratio = NULL;
}

Player::Player()
{

}
Player::~Player()
{
	printf("PLAYER CLASS DESTROYED!\n");
}

int Player::GetRank()
{
	return n_Rank;
}

int Player::GetPlayerMainAttribute()
{
	return m_MainAttribute;
}

void Player::SetAttribute(int attrib, int value)
{
	switch(attrib)
	{
	case ATTRIB_MATH:
		{
			m_AttribMath = value;
			break;
		}
	case ATTRIB_SCIENCE:
		{
			m_AttribScience = value;
			break;
		}
	case ATTRIB_PE:
		{
			m_AttribPE = value;
			break;
		}
	}
}

void Player::SetCurrentDayPoints(int value)
{
	n_CurrentDayPoints = value;
}

int Player::GetCurrentDayPoints()
{
	return n_CurrentDayPoints;
}

void Player::SubCurrentDayPoints(int value)
{
	n_CurrentDayPoints -= value;
}

void Player::AddCurrentDayPoints(int value)
{
	n_CurrentDayPoints += value;
}

int Player::GetAttribute(int attrib)
{
	if(attrib == ATTRIB_MATH)
		return m_AttribMath;
	else if(attrib == ATTRIB_SCIENCE)
		return m_AttribScience;
	else
		return m_AttribPE;
}

int Player::GetHealthPoint()
{
	return m_HealthPoints;
}

int Player::GetManaPoint()
{
	return m_ManaPoints;
}

int Player::GetSpeed()
{
	return m_Speed;
}

int Player::GetAtk()
{
	return m_Atk;
}

bool Player::GetPlayerKO()
{
	return m_IsKO;
}

int Player::GetMaxHP()
{
	return m_MaxHP;
}

int Player::GetMaxMP()
{
	return m_MaxMP;
}

core::stringw Player::GetPlayerName()
{
	return s_PlayerName;
}

void Player::DamageTaken(int dmgTaken)
{
	m_HealthPoints -= dmgTaken;
	if(m_HealthPoints <= 0)
	{
		m_HealthPoints = 0;
		m_IsKO = true;
	}
}

void Player::ManaUsed(int manaUsed)
{
	m_ManaPoints -= manaUsed;
	if(m_ManaPoints < 0)
		m_ManaPoints = 0;
}

void Player::RankUp()
{
	if(n_Rank < RANK_COUNT)
	{
		n_Rank++;
		m_isDoneRankChange = true;
	}
	n_curWinStreak = NULL;
}

void Player::RankDown()
{
	if(n_Rank > RANK_F)
	{
		n_Rank--;
		m_isDoneRankChange = true;
	}
	n_curWinStreak = NULL;
}

void Player::PlayerWin()
{
	n_curWinStreak++;
}

void Player::PlayerLose()
{
	n_curWinStreak--;
}

RCHANGE Player::isPlayerRankChange()
{
	switch(n_Rank)
	{
	case RANK_A:
		{
			if(n_curWinStreak >= RANK_A_STREAK)
			{
				RankUp();
				return RCHANGE_UP;
			}
			break;
		}
	case RANK_B:
		{
			if(n_curWinStreak >= RANK_B_STREAK)
			{
				RankUp();
				return RCHANGE_UP;
			}
			break;
		}
	case RANK_C:
		{
			if(n_curWinStreak >= RANK_C_STREAK)
			{
				RankUp();
				return RCHANGE_UP;
			}
			break;
		}
	case RANK_D:
		{
			if(n_curWinStreak >= RANK_D_STREAK)
			{
				RankUp();
				return RCHANGE_UP;
			}
			break;
		}
	case RANK_E:
		{
			if(n_curWinStreak >= RANK_E_STREAK)
			{
				RankUp();
				return RCHANGE_UP;
			}
			break;
		}
	case RANK_F:
		{
			if(n_curWinStreak >= RANK_F_STREAK)
			{
				RankUp();
				return RCHANGE_UP;
			}
			break;
		}
	}
	if(n_curWinStreak < NULL && n_Rank > RANK_F)
	{
		RankDown();
		return RCHANGE_DOWN;
	}
	return RCHANGE_NONE;
}

void Player::UpdatePlayerStats()
{
	//Initial Computation for stats. Will balance them later.
	m_Atk = ATTACK_START_VALUE + GetAttribute(GetPlayerMainAttribute())*0.5;
	m_MaxHP = HEALTH_START_VALUE + m_AttribPE*3;
	m_MaxMP = MANA_START_VALUE + m_AttribScience*2;
	m_Speed = SPEED_START_VALUE + m_AttribMath;

	//Secondary Stat Calculations
	int x = MM_CALC(1000);
	max_move = MAX_MOV + MM_CALC(m_AttribPE);
	max_attack = MAX_ATK + MA_CALC(m_AttribPE);
	movTimeLapse = MOVE_TIME_LAPSE_DEFAULT - MTL_CALC(m_Speed);//get value from save file if implemented
	atkTimeLapse = ATTACK_TIME_LAPSE_DEFAULT - ATL_CALC(m_Speed);
	atkcooldown = AC_DEFAULT - AC_CALC(m_Speed);
	movcooldown = MC_DEFAULT - MC_CALC(m_Speed);
	hitcooldown = HC_DEFAULT - HC_CALC(m_Speed);

	cur_move = max_move;
	cur_attack = max_move;
	m_HealthPoints = m_MaxHP;
	m_ManaPoints = m_MaxMP;
}

void Player::HealHP(int heal)
{
	if(m_HealthPoints + heal > m_MaxHP)
		m_HealthPoints = m_MaxHP;
	else
		m_HealthPoints += heal;
}

void Player::HealMP(int heal)
{
	if(m_ManaPoints + heal > m_MaxMP)
		m_ManaPoints = m_MaxMP;
	else
		m_ManaPoints += heal;
}